package com.wn.service.impl;

import com.wn.exception.AccountTranException;
import com.wn.mapper.AccountMapper;
import com.wn.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.security.RolesAllowed;

/**
 * @author yellow Docter
 * @date 2022 -07-08
 * @desc
 */
@Transactional(
        isolation = Isolation.DEFAULT,   //事务隔离级别,
        propagation = Propagation.REQUIRED, // 事务的传播行为
        rollbackFor = {AccountTranException.class}  // 指定事务回滚的 异常
)
@Service
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountMapper accountMapper;

    @Override
    public void transform(String outName, String inName, int money) throws Exception {

        // aa 减钱
        int row = accountMapper.outMoney(outName,money);

        System.out.println("row = " + row);
        //失败  手动抛出异常
        if (row == 0){
            throw new AccountTranException("业务逻辑问题...");
        }
        // bb 加钱
        int row1 =  accountMapper.inMoney(inName,money);
        System.out.println("row1 = " + row1);
        if (row1 == 0){
            throw new AccountTranException("业务逻辑问题...");
        }
    }
}
